<?php

class P_ImageGetAction extends BaseAction 
{
	public $id;
	public $act;
	public $unpublish;
	public $from;
	public $error_code;
    public $tag;
    public $ut;

    private $_programme_premium_flag;
    public function execute()
	{
		if(!is_null($this->error_code))
		{
			$this->addError('Image.Edit', array($this->error_code, 'Image.Edit'));
		}

		try
		{
			# add publish log
			$this->setData("publishLog", publish_log::getPublishLog('image', $this->id));
            $this->setData('special_operation_premium', TRUE);

            if(!empty($this->unpublish)) {Model::$dbo['r'] = CommonUtils::getDbConn(0,'preview');}
            
            # set tags field
			$allow_tag = CommonUtils::get_allow_tags("image");
			$tag_type  = new tag_type();
			$allow_tag_list = $tag_type->findByInSql($allow_tag);
			$tag_arr = array();
			$all_tag_list = array();
			foreach ($allow_tag_list as $tag)
			{
				$tag_arr[$tag->id] = array("id"=>$tag->id,"name"=>$tag->system_label,'restrict'=>$tag->restricted);
				$all_tag_list[$tag->id] = array();
			}
			$this->setData("allow_tag",$tag_arr);


			# image getDetails
			$image          =   new p_image();
			$country        =   new country();

			$image_detail                       =   $image->getDetails($this->id);
			$image_detail['countryList']        =   $country->getCountryList();
			$image_detail['publish_datetime']   =   CommonUtils::formatDate($image_detail['publish_datetime']);
			$image_detail['expire_datetime']    =   CommonUtils::formatDate($image_detail['expire_datetime']);

            if ($image_detail['episode_id']) {
                $episode_detail = p_episode::getEpisode($image_detail['episode_id']);
                $image_detail['onair_episode_no'] = $episode_detail->onair_episode_no;
            } else {
                $image_detail['onair_episode_no'] = '';
            }

            $image_detail['onair_episode_no_list'] = p_episode::getEpisodeNumByProgrammeId($image_detail['programme_id']);

            if ($image_detail['casts']) {
                $image_detail['casts'] = p_cast_dictionary::getCastListById(explode(',', $image_detail['casts']));
            } else {
                $image_detail['casts'] = array();
            }
            
            if(isset($image_detail['programme'])) {
                $programme_title = explode("\n\t", $image_detail['programme']);
            }

            if('ALL' == $image_detail['country_allow_list'])
            {
                $deny_flag = 1; 
                $g_country = $image_detail['country_deny_list'];
            }
            else
            {
                $deny_flag = 0;
                $g_country = $image_detail['country_allow_list'];
            }
            $image_detail['global_country'] = $g_country;
            $image_detail['global_country_allow_deny'] = $deny_flag;

            if ($this->tag) {
                $image_detail['tags'] = $this->tag;
            }

			if ($image_detail['tags']) {
				$tag_obj = new p_tag();
                $tag_list = $tag_obj->getTagList($image_detail['tags']);
                foreach ($tag_list as $tag) {
                    if(!empty($tag))
                    {
                        $all_tag_list[$tag->typeid][] = array(
                            'id'            => $tag->id, 
                            'system_label'  => htmlentities($tag->system_label, ENT_QUOTES, 'UTF-8')
                        );
                    }
                }
            }

            $this->initProgrammePremiumFlag($image_detail['programme_id']);
            $image_detail['premium_flag'] = $this->_programme_premium_flag;

            $this->setData("image", $image_detail);
			$this->setData("tag_list", $all_tag_list);
            $this->setData("programme_title",$programme_title);
			$this->setData("id", $this->id);
			$this->setData("type", 'image');
			$this->setData("act", $this->act);
			$this->setData("from", $this->from);

			return  'succ';
		}
		catch(Exception $e)
		{
			error_log("image get: {$this->id}" . var_export($e,1));

			$this->emptyData();
			return 'system-error';
		}
		

	}

    private function initProgrammePremiumFlag($programme_id)
    {
        if ($programme_id) {
            $programme = new p_programme;
            $result = $programme->get($programme_id);
            $this->_programme_premium_flag = $result->premium_flag;
        } 
    }
}
